/**
 * Copyright (c) 2012, SHX Studio.
 * All rights reserved.
 * rosary226@gmail.com
 * 
 * @author shenqianfeng
 * @date 2012-9-10 下午02:33:57   
 * @version 1.0   
 */

package com.xxx.sqfengine.texture;

import javax.microedition.khronos.opengles.GL10;

import com.xxx.sqfengine.SQFGLEnvironment;
import com.xxx.sqfengine.util.SQFGLConstants;

import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.opengl.GLUtils;

/**
 * @author shenqianfeng
 */
public class SQFGLTexture {
	
	private Bitmap mBitmap;
	private int mResId;
	private String mTextureName;
	private Context mContext;
	private int mTextureIndex;
	
	private SQFGLUv mUv;
	
	private boolean mRepeatS = false;
	private boolean mRepeatT = false;
	
	private boolean mIsEnable = false;
	
	private SQFGLTexture() {
		
	}
	
	public SQFGLTexture(String textureName, int resId) {
		mContext = SQFGLEnvironment.getContext();
		if(null == mContext) {
			SQFGLConstants.logError("context not initialized in SQFGLEnvironment!\nTexture will not be rendered properly");
			return;
		}
		mResId = resId;
		
		mTextureName = textureName;
		mUv = new SQFGLUv();
	}
	
	public SQFGLTexture(String textureName, int resId, boolean repeatS, boolean repeatT) {
		this(textureName, resId);
		this.mRepeatS = repeatS;
		this.mRepeatT = repeatT;
	}
	
	public boolean isEnabled() {
		if(mBitmap == null || ! mIsEnable) {
			return false;
		}
		return true;
	}
	
	public void setEnabled(boolean enabled) {
		mIsEnable = enabled;
		if(mIsEnable) {
			mBitmap = BitmapFactory.decodeResource(mContext.getResources(), mResId);
		}
	}
	
	public void beginDraw(GL10 gl) {

		
		// Telling OpenGL to enable textures.
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		// Tell OpenGL to enable the use of UV coordinates.
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

	}
	
	public void endDraw(GL10 gl) {
		// Disable the use of UV coordinates.
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		// Disable the use of textures.
		gl.glDisable(GL10.GL_TEXTURE_2D);
	}
	
	public void draw(GL10 gl) {
		
		// Scale up if the texture if smaller.
		gl.glTexParameterf(GL10.GL_TEXTURE_2D,
                GL10.GL_TEXTURE_MAG_FILTER,
                GL10.GL_LINEAR);
		
		// scale linearly when image smalled than texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D,
				GL10.GL_TEXTURE_MIN_FILTER,
				GL10.GL_LINEAR);

		if(mRepeatS) {
			gl.glTexParameterf(GL10.GL_TEXTURE_2D,
	                   GL10.GL_TEXTURE_WRAP_S,
	                   GL10.GL_REPEAT);
		} else {
			gl.glTexParameterf(GL10.GL_TEXTURE_2D,
	                   GL10.GL_TEXTURE_WRAP_S,
	                   GL10.GL_CLAMP_TO_EDGE);
		}
		
		if(mRepeatT) {
			gl.glTexParameterf(GL10.GL_TEXTURE_2D,
	                   GL10.GL_TEXTURE_WRAP_T,
	                   GL10.GL_REPEAT);
		} else {
			gl.glTexParameterf(GL10.GL_TEXTURE_2D,
	                   GL10.GL_TEXTURE_WRAP_T,
	                   GL10.GL_CLAMP_TO_EDGE);
		}
		
		int[] textures = new int[1];
		gl.glGenTextures(1, textures, 0);
		mTextureIndex = textures[0];
		
		// Tell OpenGL where our texture is located.
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIndex);
		
		// Telling OpenGL where our UV coordinates are.
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mUv.toUvFloatBuffer());
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);
		
		//gl.glDeleteTextures(1, textures, 0);
	}
}
